﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayTasks
{
    class Program
    {
        static void Main(string[] args)
        {
            int select;
            do
            {
                Console.Clear();
                Console.WriteLine("1 - Find maximum number in array");
                Console.WriteLine("2 - Determine the number of elements in a given array, different from the minimum to 5");
                Console.WriteLine("3 - Sum elements in array");
                Console.WriteLine("4 - Determine the value and index of the last negative element in array");
                Console.WriteLine("5 - Replace elements after maximum element by avarage value of array");
                Console.Write("Select program: ");
                select = Int32.Parse(Console.ReadLine());

                switch (select)
                {
                    case 1:
                        MaxNumberInArray();
                        break;
                    case 2:
                        ElementsInArray();
                        break;
                    case 3:
                        SumElementsInArray();
                        break;
                    case 4:
                        LastNegativeElementInArray();
                        break;
                    case 5:
                        AverageValueInArray();
                        break;
                }
            }
            while (select <= 0 || select > 5);
        }

        //Fill array from console method
        static int[] FillArrayFromConsole(int arraySize)
        {
            int[] array = new int[arraySize];
            for (int i = 0; i < arraySize; i++)
            {
                Console.Write("Enter number: ");
                array[i] = Int32.Parse(Console.ReadLine());
            }
            return array;
        }

        //Print array to console method
        static void PrintArrayToConsole(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write("{0} ", array[i]);
            }
        }

        //Find the maximum element of the array. Display the elements of an array and a maximum element.
        static void MaxNumberInArray()
        {
            Console.Clear();
            Console.WriteLine("Enter 5 numbers");
            int maxValue = Int32.MinValue;
            int[] numbers = new int[5];
            numbers = FillArrayFromConsole(5);

            //Maximum number in array
            for (int i = 0; i < 5; i++)
            {
                if (numbers[i] > maxValue)
                {
                    maxValue = numbers[i];
                }
            }

            //List elements in array
            Console.Clear();
            Console.Write("Elements in array: ");
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write("{0}, ", numbers[i]);
            }
            Console.WriteLine();
            Console.WriteLine("Maximum number is {0}", maxValue);
        }


        //Determine the number of elements in a given array, different from the minimum to 5. User enters array size on the console
        static void ElementsInArray()
        {
            Console.Clear();
            int minValue = Int32.MaxValue;
            Console.Write("Enter array capacity: ");
            int [] numbers = FillArrayFromConsole(Int32.Parse(Console.ReadLine()));

            //Minimum number in array
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.Write("Enter number: ");
                if (numbers[i] < minValue)
                {
                    minValue = numbers[i];
                }
            }
            Console.Clear();
            Console.WriteLine("Min number is {0}", minValue);

            //Count of elements different from the min to 5
            int count = 0;
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] == minValue + 5)
                {
                    count++;
                }
            }
            Console.WriteLine("Count of elements different from the minimum to 5: {0}", count);
        }


        //Find the sum of the elements located to the maximum element of the array
        static void SumElementsInArray()
        {
            Console.Clear();
            Console.Write("Enter array capacity: ");
            int sum = 0;
            int [] numbers = FillArrayFromConsole(Int32.Parse(Console.ReadLine()));
            int maxValue = numbers.Max();
            int indexOfMaxValue = Array.IndexOf(numbers, maxValue);
            for(int i = 0; i < indexOfMaxValue; i++)
            {
                sum = sum + numbers[i];
            }
            Console.WriteLine("Sum of numbers before maximum number: {0}", sum);
        }


        //Determine the value and index of the last negative element in array with size 8
        static void LastNegativeElementInArray()
        {
            Console.Clear();
            int [] numbers = FillArrayFromConsole(8);
            bool negative = false;

            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] < 0)
                {
                    negative = true;
                }
            }
            if(negative == true)
            {
                for (int i = numbers.Length - 1; i >= 0; i--)
                {
                    if (numbers[i] < 0)
                    {
                        Console.WriteLine("Last negative number in array: {0}", numbers[i]);
                        Console.WriteLine("Index of the last negative number: {0}", Array.IndexOf(numbers, numbers[i]));
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine("There aren't negative elements in array");
            }
        }


        //Replace elements after maximum element in array by avarage value of array
        static void AverageValueInArray()
        {
            Console.Clear();
            Console.Write("Enter array capacity: ");

            //Maximum number in array
            int [] numbers = FillArrayFromConsole(Int32.Parse(Console.ReadLine()));
            Console.Write("Initial array: ");
            PrintArrayToConsole(numbers);
            Console.WriteLine();

            int indexOfMaxValue = Array.IndexOf(numbers, numbers.Max());

            //Avarage value of array
            int avarage = numbers.Sum() / numbers.Length;

            //Replace elements after max element by avarage value
            for (int i = indexOfMaxValue + 1; i < numbers.Length; i++)
            {
                numbers[i] = avarage;
            }

            //Updated array
            Console.Write("Updated array: ");
            PrintArrayToConsole(numbers);
        }
    }
}
